<?php
/**
 * Object Relational Persistence Mapping Class for table: in_ticket_item
 *
 * @package	com.athena
 * @author	LHV
 */
class InTicketItem extends ORDataObject {

	/**#@+
	 * Fields of table: in_ticket_item mapped to class members
	 */
	var $in_ticket_item_id		= '';
	var $item_id		= '';
	var $enum_item_type		= '';
	var $discount		= '';
	var $original_price		= '';
	var $insurance_price		= '';
	var $patient_price		= '';
	var $ticket_id		= '';
	var $date = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'in_ticket_item';

	/**
	 * Primary Key
	 */
	var $_key = 'in_ticket_item_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'InTicketItem';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}

    public function setData($itemId, $itemType, $discount,
    $originalPrice, $insurancePrice, $patientPrice, $ticketId, $date) {
        if (!isset($date) || $date == null) {
            $date = date('Y-m-d H:i:s');

        }
        $data   =   array(
            'date'=>  $date,
            'item_id'    =>  $itemId,
            'enum_item_type'     =>  $itemType,
            'discount'          =>  $discount,
            'original_price'          =>  $originalPrice,
            'insurance_price'          =>  $insurancePrice,
            'patient_price'          =>  $patientPrice,
            'ticket_id'          =>  $ticketId
        );
        $this->populateArray($data);
    }

    public function updateData($itemId, $enumItemType, $data, $isTextItemType = true) {
    	/*
    	if ($isTextItemType) {
    	   $itemType = Utils::getEnumKey(EnumerationConstant::PARACLINIC_TYPE, $enumItemType);
    	}
    	else {
            $itemType   =   $enumItemType;
    	}*/
    	//$itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE, $enumItemType);
    	$sql = "update in_ticket_item set
                original_price = " . $data['original_price'] . ",
                patient_price = " . $data['patient_price'] . ",
                insurance_price = " . $data['insurance_price'] . "
                where item_id = '$itemId' AND enum_item_type='$enumItemType'";
        $this->_execute($sql);

    }

    public function deleteItem($itemId, $itemType) {
        $sql = "DELETE FROM $this->_table WHERE item_id ='$itemId' AND enum_item_type = '$itemType'";
        $this->_execute($sql);
    }
    public function findInTicket($itemId, $itemType) {
        $ds = new Datasource_sql();

        $sql = array(
            'cols'  => "in_ticket_item_id",
            'from'  => "$this->_table ",
            'where' => "item_id='$itemId' AND enum_item_type = '$itemType'"
            );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return null;
        }
        return $result[0];

    }

	/**
     * Get information on services a patient has used in the opening resident examination at hospital.
     *
     * @param int $patientId Patient Id
     * @return array Result array
     */
    public function listTestTicketInfoByPatient($patientId) {
        $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        
        $status     =   Ticket::OPEN_STATUS;
        $itemType = Utils::getEnumKey(EnumerationConstant::PARACLINIC_TYPE,
         EnumerationConstant::PARACLINIC_LAB);
        $sql    =   array(
            'cols'  =>  "t.date issue_date, tdt.requested_time requested_time, test_diagnosis_ticket_id, 
                    t.original_price, t.insurance_price, t.patient_price, tdt.doctor_id, p.name as doctor, 
                    tk.department_id, d.$field department_name",
            'from'  =>  "$this->_table t
                INNER JOIN ticket tk ON (t.ticket_id=tk.ticket_id AND t.enum_item_type='$itemType')
                LEFT JOIN department d ON (d.department_id=tk.department_id)                
                LEFT JOIN test_diagnosis_ticket tdt ON (tdt.test_diagnosis_ticket_id=t.item_id)
                LEFT JOIN person p ON (tdt.doctor_id=p.person_id)",
            'where' =>  "tk.patient_id=$patientId AND tk.status='$status'",
            'orderby'  => "t.date ASC"
        );

        $ds     =   & new Datasource_sql();
        $ds->setup($this->_db, $sql);

        return $ds->toArray();
    }
    public static function normalizeTestTicketDataForDisplay(&$testTickets) {

    	foreach ($testTickets as &$testTicket) {
    		$testTicket['issue_date'] = L10nHelper::renderDateTime($testTicket['issue_date'],'short', 'datetime');
    		$testTicket['requested_time'] = L10nHelper::renderDateTime($testTicket['requested_time'],'short', 'datetime');
    		$testTicket['original_price'] = L10nHelper::renderNumber($testTicket['original_price'],0);
            $testTicket['insurance_price'] = L10nHelper::renderNumber($testTicket['insurance_price'],0);
            $testTicket['patient_price'] = L10nHelper::renderNumber($testTicket['patient_price'],0);


    	}

    }
}
?>
